package com.junjun.ssm.service;

import org.springframework.stereotype.Service;

import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;

@Service
@ServerEndpoint("/mySocket")
public class WsService {
    Session session;
    @OnOpen
    public void onOpen(Session session) {
     this.session=session;
    }
    /**
     * 链接关闭调用的方法
     */
    @OnClose
    public void onClose() {

    }
    /**
     * 收到客户端消息后调用的方法
     *
     * @param message
     */
    @OnMessage
    public void onMessage(String message) throws IOException, InterruptedException {
     int i=0;

      while (message.equals("start")) {
          Thread.sleep(1000);
          System.out.println(message);
          //向前端发送数据
          session.getBasicRemote().sendText(String.valueOf(i));

          i=i+10;

      }
    }

    /** 发送错误时的处理
     * @param session
     * @param error
     */
    @OnError
    public void onError(Session session, Throwable error) {


    }
}
